import { Component, OnInit } from '@angular/core'
import { Location } from '@angular/common'
import { log } from 'util';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'loan-detail',
  templateUrl: './loan-detail.component.html',
  styleUrls: ['./loan-detail.component.scss'],
})
export class LoanDetailComponent implements OnInit {

    Data:any;
    isclick = true;
    // 等额本金
    averageCapital: any= { }
    // 等额本息
    averageRate:any = { }
    chartOption:any = { }
    


    constructor( private activatedRoute: ActivatedRoute, private location: Location, ) { }

    ngOnInit() {

        this.activatedRoute.queryParams.subscribe(queryParams => {

            this.Data = {
                total: queryParams.total,  
                type: queryParams.type,
                money: queryParams.money,
                businessLoan: JSON.parse(queryParams.businessLoan),
                businessLoanRate: JSON.parse(queryParams.businessLoanRate),
                fundLoan: JSON.parse(queryParams.fundLoan),
                fundLoanRate: JSON.parse(queryParams.fundLoanRate),
            }
            // console.log(this.Data);
            this.initTabs()
            this.delaytime()
        })
    }

    delaytime(){
        
        setTimeout(() => {
            if(this.isclick == true){
                this.chartOption = {
                    
                    color: ['#4C95EF','#FB6161'],
                    series: [
                        {
                            name:'访问来源',
                            type:'pie',
                            hoverAnimation: false,
                            radius: ['70%', '100%'],
                            avoidLabelOverlap: false,
                           
                            label: {
                                normal: {
                                    show: false,
                                    position: 'center'
                                },
                                emphasis: {
                                    show: false,
                                    textStyle: {
                                        fontSize: '20',
                                        fontWeight: 'bold'
                                    }
                                }
                            },
                            labelLine: {
                                normal: {
                                    show: false
                                }
                            },
                            data:[
                                
                                {value: this.averageRate.loanCeiling, name:'贷款总额'},
                                {value:this.averageRate.payAccrual, name:'支付利息'}
                            ]
                        }
                    ]
                } 

            } else {

                this.chartOption = {
                    tooltip: {
                        trigger: 'item',
                        formatter: "{b}: {c} ({d}%)",
                        showContent :false,
                    },
                    legend: {
                        orient: 'vertical',
                        x: 'right',
                        y: '100px',
                        left: '420px',
                        selectedMode: false,
                        Color: ['red','blue'],
                        // data:['贷款总额','支付利息']
                    },
                    color: ['#4C95EF','#FB6161'],
            
                    series: [
                        {
                            name:'访问来源',
                            type:'pie',
                            hoverAnimation: false,
                            radius: ['70%', '100%'],
                            avoidLabelOverlap: false,
                            label: {
                            
                                normal: {
                                    show: false,
                                    position: 'center'
                                },
                                emphasis: {
                                    show: false,
                                    textStyle: {
                                        fontSize: '20',
                                        fontWeight: 'bold'
                                    }
                                }
                            },
                            labelLine: {
                                normal: {
                                    show: false
                                }
                            },
                            data:[
                                
                                {value: this.averageCapital.loanCeiling, name:'贷款总额'},
                                {value:this.averageCapital.payAccrual, name:'支付利息'}
                            ]
                        }
                    ]
                } 
            }
            
        }, 50)
        
    }

    initTabs() {

		var that = this;
		var data = this.Data;

		var total = Number(data.total) - Number(data.money);//贷款总额
		var type = data.type; //类型 Mix 组合贷款，Business 商业贷款，Fund 公积金贷款
		var businessLoan = data.businessLoan;
		var businessLoanRate = data.businessLoanRate;
		var fundLoan = data.fundLoan;
		var fundLoanRate = data.fundLoanRate;

		var interestPay1 = 0;//支付总利息 等额本息
		var interestPay2 = 0;//支付总利息
		var month = 0;
		var monthBack1 = 0;//月均还款 等额本息
		var monthBack2 = 0;//月均还款 等额本金
		var descending = 0;//每月递减 等额本金
		var borrow;//等额本息
		var floan;//等额本金
		if (type == 'Business') {

			borrow = this.borrow(total * 10000, businessLoanRate.Value, businessLoan.Year.Value);
			floan = this.floan(total * 10000, businessLoanRate.Value, businessLoan.Year.Value);

			month = businessLoan.Year.Value * 12;
            interestPay1 = Math.ceil(borrow.totalInterest / 10000);
          
			monthBack1 = parseInt(borrow.monthBack);

			interestPay2 = Math.ceil(floan[floan.length - 1] / 10000) - total;
			monthBack2 = parseInt(floan[0]);
			descending = (Math.round((floan[0] - floan[1]) * 100)) / 100;
		} else if (type == 'Fund') {

			borrow = this.borrow(total * 10000, fundLoanRate.Value, fundLoan.Year.Value);
			floan = this.floan(total * 10000, fundLoanRate.Value, fundLoan.Year.Value);

			month = fundLoan.Year.Value * 12;
			interestPay1 = Math.ceil(borrow.totalInterest / 10000);
			monthBack1 = parseInt(borrow.monthBack);

			interestPay2 = Math.ceil(floan[floan.length - 1] / 10000) - total;
			monthBack2 = parseInt(floan[0]);
			descending = (Math.round((floan[0] - floan[1]) * 100)) / 100;
		} else {

			borrow = this.mixBorrow(businessLoanRate, businessLoan, fundLoanRate, fundLoan);
			floan = this.mixFloan(businessLoanRate, businessLoan, fundLoanRate, fundLoan);
			month = businessLoan.Year.Value * 12;

			interestPay1 = Math.ceil(borrow.totalInterest / 10000);
			monthBack1 = parseInt(borrow.monthBack);

			interestPay2 = Math.ceil(floan[floan.length - 1] / 10000) - total;
			monthBack2 = parseInt(floan[0]);
			descending = (Math.round((floan[0] - floan[1]) * 100)) / 100;
        }
        // 等额本金
        this.averageCapital = {
            'fristPay': data.money,  // 首付
            'loanCeiling': total,   // 贷款总额
            'refundAmount': total + interestPay2,  // 还款总额
            'payAccrual': interestPay2,  // 支付利息
            'loanMonth': month,    // 贷款月数
            'fristMonth': monthBack2,  // 首月还款
            'monthReduce': descending   // 每月递减
        }
        // 等额本息
        this.averageRate = {
            'fristPay': data.money,  // 首付
            'loanCeiling': total,   // 贷款总额
            'refundAmount': total + interestPay1,  // 还款总额
            'payAccrual': interestPay1,  // 支付利息
            'loanMonth': month,    // 贷款月数
            'monthRefund': monthBack1,  // 月均还款
        }
        // console.log(this.averageCapital);
        // console.log(this.averageRate);
        
	}

	mixBorrow(businessLoanRate, businessLoan, fundLoanRate, fundLoan) {

		var borrow1 = businessLoan.Money.Value > 0 && this.borrow(businessLoan.Money.Value * 10000, businessLoanRate.Value, businessLoan.Year.Value);
		var borrow2 = fundLoan.Money.Value > 0 && this.borrow(fundLoan.Money.Value * 10000, fundLoanRate.Value, fundLoan.Year.Value);

		if (borrow1 && borrow2) {

			return {
				monthBack: borrow1.monthBack + borrow2.monthBack,
				totalBack: borrow1.totalBack + borrow2.totalBack,
				monthInterest: borrow1.monthInterest + borrow2.monthInterest,
				totalInterest: borrow1.totalInterest + borrow2.totalInterest,
			};

		} else if (borrow1 && !borrow2) {

			return borrow1;
		} else if (borrow2 && !borrow1) {

			return borrow2;
		}
	}
	mixFloan(businessLoanRate, businessLoan, fundLoanRate, fundLoan) {

		var floan1 = businessLoan.Money.Value > 0 && this.floan(businessLoan.Money.Value * 10000, businessLoanRate.Value, businessLoan.Year.Value);
		var floan2 = fundLoan.Money.Value > 0 && this.floan(fundLoan.Money.Value * 10000, fundLoanRate.Value, fundLoan.Year.Value);

		if (floan1 && floan2) {

			return floan1.map(function (item, index) {

				item += floan2[index];
				return item;
			})
		} else if (floan1 && !floan2) {

			return floan1;
		} else if (floan2 && !floan1) {

			return floan2;
		}
	}
	//等额本息还款法
	//original贷款金额
	//yearratio年利率%，如年利率5.6%就为5.6
	//year还款年限
	borrow(original, yearratio, year) {
		//验证数据有限性
		year = parseInt(year);
		original = parseFloat(original);
		yearratio = parseFloat(yearratio);

		//还款月数
		var timeSpan = year * 12;
		//某种利率
		var active = yearratio * 10 / 12 * 0.001;

		var t1 = Math.pow(1 + active, timeSpan);
		var t2 = t1 - 1;
		var tmp = t1 / t2;
		//月利率
		var monthratio = active * tmp;

		//每月支付本息
		var monthBack = original * monthratio;
		//累计还款总额
		var totalBack = monthBack * timeSpan;
		//累计支付利息
		var totalInterest = totalBack - original;
		//每月应付利息
		var monthInterest = totalInterest / timeSpan;

		totalInterest = (Math.round(totalInterest * 100)) / 100;//累计支付利息：取两位小数
		monthInterest = (Math.round(monthInterest * 10000)) / 10000;//每月应付利息：取两位小数
		monthBack = (Math.round(monthBack * 10000)) / 10000;//每月支付本息：取两位小数
		totalBack = (Math.round(totalBack * 100)) / 100;//累计还款总额：取两位小数

		return {
			monthBack: monthBack,
			totalBack: totalBack,
			monthInterest: monthInterest,
			totalInterest: totalInterest
		};
	}
	//等额本金还款法
	//original贷款金额
	//yearratio年利率%，如年利率5.6%就为5.6
	//year还款年限
	floan(original, yearratio, year) {

		//验证数据有限性
		year = parseInt(year);
		original = parseFloat(original);
		yearratio = parseFloat(yearratio);

		//还款月数
		var timeSpan = year * 12;
		//某种利率
		var active = yearratio * 10 / 12 * 0.001;

		var objArray = new Array();
		//月还款额
		var interestM = 0;
		//累计还款总额
		var interestTotal = 0;
		for (var i = 1; i < timeSpan + 1; i++) {
			var t1 = (original - original * (i - 1) / timeSpan) * active;//第i月还款利息
			interestM = original / timeSpan + t1;//第i月还款额
			objArray[i - 1] = interestM;
			interestTotal = interestTotal + interestM;
		}

		interestTotal = (Math.round(interestTotal * 100)) / 100
		objArray[timeSpan] = interestTotal;
		//alert(objArray);
		return objArray;
	}
      
    selc1(){
        // console.log(this.Data);
        this.isclick = true;
        this.delaytime()
    }
    selc2(){

        this.isclick = false;
        this.delaytime()
    }  

    onClickLeft() {
        this.location.back()
    }


}